package com.testmodelconnectionapp;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
 * Created by 张英杰 on 2018/5/31.
 * https://www.jianshu.com/p/d1fac6ccee98
 */

public class AIDLService extends Service {
    private static final String TAG = "AIDLService";
    private boolean mFlag = true;

    private Handler mhandler = new Handler(){};

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
        Log.i(TAG, "onCreate: mFlag=="+mFlag);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 当客户端绑定到该服务时 会执行
     * @param intent
     * @return
     */
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        return iBinder;
    }

    private MClientAIDL mClientAIDL;
    private IBinder iBinder = new MServiceAIDL.Stub() {

//        @Override
//        public String getClientInfo(int num1, int num2, MsgBean inBean, MsgBean outBean, MsgBean inoutBean, String str) throws RemoteException {
//            Log.i(TAG, "接收到Client的信息: ");
//            Log.i(TAG, "getClientInfo: in msg "+ inBean.toString());
//            Log.i(TAG, "getClientInfo: out msg "+ outBean!=null ? outBean.toString() :"null");
//            Log.i(TAG, "getClientInfo: inout msg "+ outBean!=null ? inoutBean.toString() :"null");
//
//            inBean.setMsg1(inBean.getMsg1()+"  deal with servicer");
//            outBean.setMsg1(outBean.getMsg1()+"  deal with servicer");
//            inoutBean.setMsg1(inoutBean.getMsg1()+"  deal with servicer");
//
//            mClientAIDL.getServerInfo(inBean,"msgg Form Server");
//            return String.valueOf(num1+num2);
//        }

        @Override
        public String getClientInfo(int num1, int num2, MsgBean inBean) throws RemoteException {
            Log.i(TAG, "接收到Client的信息: ");
            Log.i(TAG, "getClientInfo: in msg "+ inBean.toString());
            inBean.setMsg1(inBean.getMsg1()+"  deal with servicer");
            mClientAIDL.getServerInfo(inBean,"msgg Form Server");
            return String.valueOf(num1+num2);
        }

        @Override
        public void addBeanOut(MsgBean outBean) throws RemoteException {
            Log.i(TAG, "接收到Client的信息: ");
            Log.i(TAG, "getClientInfo: out msg "+ outBean!=null ? outBean.toString() :"null");
            outBean.setMsg1(outBean.getMsg1()+"  deal with servicer");
        }

        @Override
        public void addBeanInOut(MsgBean inoutBean) throws RemoteException {
            Log.i(TAG, "接收到Client的信息: ");
            Log.i(TAG, "getClientInfo: inout msg "+ inoutBean!=null ? inoutBean.toString() :"null");
            inoutBean.setMsg1(inoutBean.getMsg1()+"  deal with servicer");
        }

        @Override
        public MsgBean getServerInfo(MClientAIDL clientAIDL) throws RemoteException {
            //收到客户端的binder
            mClientAIDL = clientAIDL;

            mhandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        float[] floats = mClientAIDL.getFloats(" i am TestModelConnectionAPP AIDLService.");
                        if (floats != null) {
                            for (int i = 0; i < floats.length; i++) {
                                Log.i(TAG, "run: floats: "+floats[i]);
                            }
                        }else {
                            Log.e(TAG, "run: mClientAIDL.getFloats floats == null");
                        }

                        MsgBean msgBean = new MsgBean();
                        msgBean.setMsg1("我是来自Server的Msg1");
                        mClientAIDL.getServerInfo(msgBean,"msgg Form Server");
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            },1000);
            return null;
        }

        @Override
        public void setFlag(boolean flag) throws RemoteException {
            mFlag = flag;
            Log.i(TAG, "setFlag: mFlag=="+mFlag);

        };


    };

    }


